import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class FlutterDouyin {
  static const MethodChannel _channel = MethodChannel('flutter_douyin');

  static Future<String?> get platformVersion async {
    final String? version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }

  /**
   * 设置抖音抖音的key
   */
  static Future<String?> setKey(String androidKey, String iosKey) async {
    final String? platformKey = await _channel.invokeMethod(
        'setPlatformKey', {'androidKey': androidKey, 'iosKey': iosKey});
    return platformKey;
  }
  static Future<String> DouyinOpenSDKShareRequest(dynamic arguments) async {
    _channel.setMethodCallHandler(handler);
    return await _channel.invokeMethod('douyinOpenSDKShareRequest',arguments);
  }


  /**
   * 抖音授权
   */
  static Future<String> DouyinOpenSDKAuthRequest(dynamic arguments) async {
    _channel.setMethodCallHandler(handler);
    return await _channel.invokeMethod('douyinOpenSDKAuthRequest',arguments);
  }

  static StreamController<Map> _locationUpdateStreamController =
      new StreamController.broadcast();

  static Stream<Map> get authUpdate => _locationUpdateStreamController.stream;

  static Future<dynamic> handler(MethodCall call) {
    String method = call.method;

    switch (method) {
      case "ANDROID_DouyinOpenSDKAuthRequest":
        {
          print("Android DouyinOpenSDKAuthRequest 授权返回:" + "${call.arguments}");
          _locationUpdateStreamController.add(call.arguments);
        }
        break;
      case "ISO_DouyinOpenSDKAuthRequest": //IOS 抖音授权返回
        {
          print("IOS DouyinOpenSDKAuthRequest 授权返回:" + "${call.arguments}");
          _locationUpdateStreamController.add(call.arguments);
        }
        break;
      case "ISO_DouyinOpenSDKShareRequest": //IOS 抖音授权返回
        {
          print("IOS DouyinOpenSDKShareRequest 分享返回:" + "${call.arguments}");
          _locationUpdateStreamController.add(call.arguments);
        }
        break;
    }
    return new Future.value("");
  }
}
